package com.xiaoyu.merchant.ui.fragment.goods;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseFragment;
import com.xiaoyu.merchant.MerchantConstants;
import com.xiaoyu.merchant.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品详情页中的累计评价fragment
 */
public class GoodsCommentFragment extends BaseFragment {
    @BindView(R.id.page_list_layout)
    RecyclerView mListLayout;
    @BindView(R.id.page_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.page_list_no_data_layout)
    RelativeLayout mNoDataImg;

    private AllPowerfulAdapter mAdapter;
//    private List<CommentParam> mList = new ArrayList<>();
    private View mView;
    private String goodsId;//商品id
    private int page = 1;
    private int totalPage;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = LayoutInflater.from(getActivity()).inflate(R.layout.page_recycler_list, container, false);
            ButterKnife.bind(this, mView);
            initView();
            getCommentInfo();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        return mView;
    }


    private void initView() {
        Bundle bundle = getArguments();
        goodsId = bundle.getString(MerchantConstants.GOODS_ID);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mListLayout.setLayoutManager(layoutManager);
//        setAdapter(mList);

        mRefreshLayout.setOnRefreshListener(onRefreshListener);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        mRefreshLayout.setNoMoreData(true);
    }

//    private void setAdapter(List<CommentParam> mList) {
//        mAdapter = new AllPowerfulAdapter<CommentParam>(R.layout.item_goods_comment,mList){
//            @Override
//            protected void convert(BaseViewHolder baseViewHolder, CommentParam param) {
//                super.convert(baseViewHolder, param);
//                CCircleImageView portraitImg = baseViewHolder.getView(R.id.item_comment_portrait_img);
//                Glide.with(getActivity()).load(param.getUserphoto()).into(portraitImg);//头像
//                baseViewHolder.setText(R.id.item_comment_name,param.getNickname());//昵称
//                RatingBar ratingBar = baseViewHolder.getView(R.id.item_comment_score);
//                ratingBar.setStar(Float.valueOf(param.getDivide()));//星级
//                baseViewHolder.setText(R.id.item_comment_content,param.getTitle());//文字评价
//                LinearLayout imglayout = baseViewHolder.getView(R.id.item_comment_img_layout);//图片评价
//                if ( param.getImg().size() == 0){
//                    imglayout.setVisibility(View.GONE);
//                }else {
//                    imglayout.setVisibility(View.VISIBLE);
//                }
//                for (int i = 0;i < param.getImg().size();i++){
//                    if (i < imglayout.getChildCount()){
//                        ImageView imageView = (ImageView) imglayout.getChildAt(i);
//                        Glide.with(getActivity()).load(param.getImg().get(i)).into(imageView);
//                    }
//                }
//
//                baseViewHolder.setText(R.id.item_comment_time,param.getGoodspingjiatime());//评价时间
//            }
//        };
//        mListLayout.setAdapter(mAdapter);
//    }


    /**
     * 刷新
     */
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
//            mList.clear();
            mAdapter.notifyDataSetChanged();
            getCommentInfo();
            mRefreshLayout.finishRefresh();
        }
    };

    /**
     * 加载更多
     */
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page < totalPage){
                page = page + 1;
                getCommentInfo();
                mRefreshLayout.setNoMoreData(false);
                mRefreshLayout.finishLoadMore();
            }else {
                mRefreshLayout.finishLoadMore();
                mRefreshLayout.setNoMoreData(true);
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    };

    /**
     * 获取详情信息
     */
    private void getCommentInfo() {
//        NetworkManager.getGoodsCommentList(page, goodsId, new CommonHttpResponser() {
//            @Override
//            public void onSuccess(String result) {
//                parseCommentInfoSuc(result);
//            }
//
//            @Override
//            public void onFailed(String errorCode, String errorDiscription) {
//                ToastUtil.logError("商品评价",errorCode,errorDiscription);
//            }
//        });
    }

    private void parseCommentInfoSuc(String result) {
//        ToastUtil.logResult("商品评价",result);
//        if (page == 1){
//            mList.clear();
//        }
//        CommentBean bean = new Gson().fromJson(result,CommentBean.class);
//        totalPage = Integer.parseInt(bean.getData().getCount());
//        mList.addAll(bean.getData().getList());
//        if (totalPage == 0){
//            mNoDataImg.setVisibility(View.VISIBLE);
//            mRefreshLayout.setVisibility(View.GONE);
//        }else if (totalPage == 1){
//            mRefreshLayout.setEnableLoadMore(false);
//        }else {
//            mRefreshLayout.setEnableLoadMore(true);
//        }
//        mAdapter.notifyDataSetChanged();
    }

}
